/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.mi.shared.util;

import org.bmi.gwt.mi.shared.MiObject;
import org.bmi.gwt.mi.shared.MiProxy;
import org.bmi.gwt.mi.shared.MiPublisher;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;

public class ProxyBase
{
	protected MiPublisher publisher;
	protected String objectId;

	protected Class<? extends MiObject> targetMiObjectClass;

	protected String constrSignature;
	protected ProxyConstructor constrPublisher;

	public interface ProxyConstructor
	{
		void doConstructor(Object[] args) throws InvalidArgumentException;
	}

	/**
	 * Publish the constructor invocation of the target object.
	 * 
	 * @param proxy
	 * @param args
	 * @throws InvalidArgumentException
	 */
	public static void publishConstructor(ProxyBase proxy, Object[] args) throws InvalidArgumentException
	{
		proxy.constrPublisher.doConstructor(args);
	}

	/**
	 * Sets the provided publisher object to a given {@link ProxyBase} element.
	 * 
	 * @param proxy
	 * @param publisher
	 */
	public static void setPublisher(ProxyBase proxy, MiPublisher publisher)
	{
		proxy.publisher = publisher;
	}

	/**
	 * Returns the signature of the constructor arguments.
	 * 
	 * @param proxy Target proxy element.
	 * @return The signature in the form of a string.
	 */
	public static String getConstructorSignature(ProxyBase proxy)
	{
		return proxy.constrSignature;
	}

	/**
	 * Returns the target object class, required if a client makes a constructor
	 * request.
	 * 
	 * @param proxy A proxy element which should implements {@link MiProxy} and
	 *        extends {@link ProxyBase}.
	 * @return The class of the target object.
	 */
	public static Class<? extends MiObject> getTargetMiObjectClass(ProxyBase proxy)
	{
		return proxy.targetMiObjectClass;
	}

	/**
	 * Sets the object id of a given proxy.
	 * 
	 * @param proxy Target proxy element.
	 * @param objectId The object id.
	 */
	public static void setObjectId(ProxyBase proxy, String objectId)
	{
		proxy.objectId = objectId;
	}

	/**
	 * Returns the object id of a given proxy.
	 * 
	 * @param proxy Target proxy element;
	 * @return The object id.
	 */
	public static String getObjectId(ProxyBase proxy)
	{
		return proxy.objectId;
	}
}
